package com.community.service;

import com.community.entity.Member;
import com.community.entity.Student;
import com.community.entity.VipOrder;
import com.community.util.Md5Util;
import com.community.util.Search;
import com.community.util.SessionUtil;
import com.feng.fengDUtil.BaseService;
import com.feng.fengDUtil.Page;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class StudentService extends BaseService {
    public static final String SALT = "beerg";
    /**
     * 根据账户查询用户信息
     * @param username 账户
     * @return 用户信息
     */
    public Student findStudent(String username) {
        return sql("select * from a_user where username = ? ").one(Student.class,username);
    }

    public Student getByOpenid(String openid) {
        return sql("select * from a_user where open_id = ? ").one(Student.class,openid);
    }

    public Student findStudent(String username,String password) throws Exception {
        Student student = findStudent(username);
        if(student!=null && Md5Util.encodePassword(password,SALT).equalsIgnoreCase(student.getPassword())){
            return student;
        }else{
            throw new Exception("此用户名或密码错误！");
        }
    }

    public Student curStudent(){
        String studentId = SessionUtil.getCurStudentId();
        if(studentId!=null){
            return selectById(Student.class, studentId);
        }
       return null;
    }



   /* public List<Map> getMenuTree(User user){
        List<Map> list = sql("select * from menu").list(Map.class);
        return TreeUtil.geneTree(list);
    }*/
    
    /**
     * 修改密码
     * @param username 账户
     * @param newPassword 新密码
     * @return 用户
     */
    public void changePassword(String username,String newPassword) {
		// 获取账户匹配的用户信息
		Student student = findStudent(username);
		// 判断原密码与输入的密码是否一致
		List<Object[]> params = new ArrayList<>();
        params.add(new Object[]{Md5Util.encodePassword(newPassword,SALT),username});
		String sql = "update a_user set password=? where account=?";
		batch(sql,params);
	}

    public Page query(Page page, Search search) {
        ArrayList params = new ArrayList();
        String sql = "select u.*," +
                "(select count(*) From a_user_collection c,a_major a where c.user_id = u.id and c.major_id = a.id  ) attentionNum" +
                " from a_user u where 1=1 ";

        if(search.getIsVip()!=null){
            if(search.getIsVip()>0){
                sql+=" and u.vip_level = 1";
            }else{
                sql+=" and ( u.vip_level is null or u.vip_level = 0)";
            }
        }
        if(StringUtils.isNotEmpty(search.getName())){
            sql+=" and u.name like ? ";
            params.add(processLike(search.getName()));
        }

        if(StringUtils.isNotEmpty(search.getPhone())){
            sql+=" and u.phone like ? ";
            params.add(processLike(search.getPhone()));
        }

        sql+=" order by u.create_date desc";
        findPageList(Map.class,sql,page,params.toArray());
        return page;
    }

    public void setVip(String id){
        sql("update a_user set vip_level = 1 where id = ?").update(new Object[]{id});
    }

    public void vipViaCode(Student student, String code) throws Exception {
        Member member = sql("select * from a_member where content = ? and delete_flag = 1").one(Member.class,code);
        if(member == null){
            throw new Exception("激活码不存在!");
        }
        member.setDelete_flag(false);
        update(member);
        student.setVip_level(1);
        student.setDefault_vip(code);
        update(student);
    }

    //写预支付订单信息
    public void saveVipOrder(Student student,BigDecimal amount,String orderId){
        log.debug(student.getOpen_id()+"：创建支付订单");
        VipOrder vipOrder=new VipOrder();
        vipOrder.setAmount(amount);
        vipOrder.setStatus(0);
        vipOrder.setOpen_id(student.getOpen_id());
        vipOrder.setUid(student.getId());
        vipOrder.setOrder_id(orderId);
        vipOrder.setTime(new Date());
        insert(vipOrder);
    }

    public void payComplete(String orderId,String openid){
        sql("update a_vip_order set status = 1,pay_time=? where order_id=?").update(new Object[]{new Date(),orderId});
        sql("update a_user set vip_level=1 where open_id = ?").update(new Object[]{openid});
    }



}
